var RunLayer = cc.Layer.extend({
	arr:null,
	scene:null,
	clock:null,
	ctor:function (parent) {
		this._super();
		this.scene = parent;
		this.scene.addChild(this, 10);
		gg.main = this;
		this.init();
	},
	init:function () {
		this.callNext = cc.callFunc(function(){
			gg.flow.next();
		}, this);
		this.callKill = cc.callFunc(function(p){
			var seq = cc.sequence(cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);	
			}, this));
			p.runAction(seq);
		}, this); 
		
		this.callNext.retain();
		this.callKill.retain();
		this.clock = new Clock(this);
		this.water = new Water(this);
		
		this.lib = new Lib(this);
	},
	loadZilaishui:function(pos){
		this.zilaishui = new ZiLaiShui(this);
		this.addChild(this.zilaishui, 20);
		this.loadInLib(this.zilaishui, pos, cc.p(gg.width*0.7, gg.height*0.5));
	},
	loadZhengliushui:function(pos){
		this.zhengliushui = new ZhengLiuShui(this);
		this.addChild(this.zhengliushui, 20);
		this.loadInLib(this.zhengliushui, pos, cc.p(gg.width*0.8, gg.height*0.5));
	},
	loadIron:function(pos){
		this.ironNode = new cc.Node();
		this.ironNode.setCascadeOpacityEnabled(true);
		this.addChild(this.ironNode,20);
		
		var iron = new Button(this.ironNode, 5, TAG_IRON, "#ironShelf.png");
		this.loadInLib(iron, pos, cc.p(gg.width*0.15, gg.height*0.4));
	},
	loadDidingguan:function(pos){
		this.didingguan = new Didingguan(this);
		this.addChild(this.didingguan, 51);
		this.loadInLib(this.didingguan, pos, cc.p(gg.width*0.5, gg.height*0.3));
		this.didingguan.setScale(0.7);
	},
	loadXishuizhi:function(pos){
		var xishuizhi = new Button(this, 22, TAG_XISHUIZHI, "#xishuizhi.png", this.callback);
		xishuizhi.setPosition(cc.p(1100,550));
		if(gg.flow.flow.action==ACTION_DO1){
			var move=cc.moveTo(0.6,cc.p(gg.width*0.5+5, gg.height*0.3+150));
			var move1=cc.moveTo(0.6,cc.p(gg.width*0.5+5, gg.height*0.3-150));
			var fad=cc.fadeOut(0);
			var seq=cc.sequence(move1,move,move1,fad,cc.callFunc(function() {
				xishuizhi.removeFromParent();
			}, this),this.callNext);
			xishuizhi.runAction(seq);
		}else{
			var move=cc.moveTo(0.6, cc.p(500, 430));
			var move1=cc.moveTo(0.6, cc.p(500, 280));
			var seq=cc.sequence(move,move1,move,cc.callFunc(function() {
				xishuizhi.removeFromParent();
			}, this),this.callNext);
			xishuizhi.runAction(seq);
		}
	},
	loadBeaker:function(pos){
		var action=gg.flow.flow.action;
		if(action==ACTION_DO1){
			var beaker = new Beaker(this, 50, TAG_BEAKER,"#feiyegang.png",this.callback); 
			this.loadInLib(beaker, pos, cc.p(gg.width*0.65, gg.height*0.2-1));
			var fei=new cc.LabelTTF("废液缸","Arial","14");
			this.getChildByTag(TAG_BEAKER).addChild(fei,11);
			fei.setColor(cc.color(0,0,0));
			fei.setPosition(cc.p(30,42));
		}else if(action==ACTION_DO2){
			var beaker = new Beaker(this, 10, TAG_BEAKER2,"#beaker/beaker.png", this.callback); 
			this.loadInLib(beaker, pos, cc.p(gg.width*0.75, gg.height*0.2));
		}else{
			var beaker = new Beaker(this, 10, TAG_BEAKER3,"#beaker/beaker.png", this.callback); 
			this.loadInLib(beaker, pos, cc.p(gg.width*0.75, gg.height*0.2));
		}			
	},
	loadHcl:function(pos){
		this.hcl = new Hcl(this);
		this.addChild(this.hcl, 20);
		this.loadInLib(this.hcl, pos, cc.p(gg.width*0.9, gg.height*0.5));
	},
	loadYiyeguan:function(pos){
		this.yiyeguan = new Yiyeguan(this);
		this.addChild(this.yiyeguan, 51);
		this.loadInLib(this.yiyeguan, pos, cc.p(570, 250));
		this.yiyeguan.setScale(0.8);
	},
	loadJianye:function(pos){
		this.jianye = new Jianye(this);
		this.addChild(this.jianye, 52);
		this.loadInLib(this.jianye, pos, cc.p(gg.width*0.75, gg.height*0.2));
	},
	loadXierqiu:function(){
		var action=gg.flow.flow.action;
		var xierqiu = new Button(this, 52, TAG_XIERQIU, "#xierqiu1.png", this.callback);
		xierqiu.setPosition(cc.p(1100,563));
		xierqiu.setScale(0.72);
		
		var yyg=this.getChildByTag(TAG_YYG_YYG);
		var beaker=this.getChildByTag(TAG_BEAKER);
		var beaker2=this.getChildByTag(TAG_BEAKER2);
		var beaker3=this.getChildByTag(TAG_BEAKER3);
		
		if(action==ACTION_DO1){
			var move=cc.moveTo(0.6, cc.p(933,563));
			var move1=cc.moveTo(0.6,cc.p(933,523));
			var seq=cc.sequence(move,move1,cc.callFunc(function() {
				xierqiu.setSpriteFrame("xierqiu2.png");
				yyg.yyg_lineUp(cc.p(21,210));
				beaker2.lineDown(false,cc.p(0,-5));
			}, this),cc.delayTime(2.3),cc.callFunc(function() {
				xierqiu.removeFromParent();
				var hand=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT);
				var part=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART);
				hand.setVisible(false);
				part.setVisible(false);
				var hand1=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT1);
				var part2=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART2);
				hand1.setVisible(true);
				part2.setVisible(true);				
			}, this),this.callNext);
			xierqiu.runAction(seq);					
		}else if(action==ACTION_DO2){
			var move=cc.moveTo(0.6, cc.p(933,563));
			var move1=cc.moveTo(0.6,cc.p(933,523));
			var seq=cc.sequence(move,move1,cc.callFunc(function() {
				xierqiu.setSpriteFrame("xierqiu2.png");
				yyg.yyg_lineUp(cc.p(21,210));
				beaker3.lineDown(false,cc.p(0,-5));
			}, this),cc.delayTime(2.3),cc.callFunc(function() {
				xierqiu.removeFromParent();
				var hand=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT);
				var part=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART);
				hand.setVisible(false);
				part.setVisible(false);
				var hand1=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT1);
				var part2=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART2);
				hand1.setVisible(true);
				part2.setVisible(true);			
				hand1.setSpriteFrame("yyg_hand1.png");
			}, this),this.callNext);
			xierqiu.runAction(seq);	
		}else if(action==ACTION_DO3){
			var move=cc.moveTo(0.6, cc.p(933,563));
			var move1=cc.moveTo(0.6,cc.p(933,523));
			var seq=cc.sequence(move,move1,cc.callFunc(function() {
				xierqiu.setSpriteFrame("xierqiu2.png");
				yyg.yyg_lineUp(cc.p(21,210));
			}, this),cc.delayTime(2.3),cc.callFunc(function() {
				xierqiu.removeFromParent();
				var hand=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT);
				var part=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART);
				hand.setVisible(false);
				part.setVisible(false);
				var hand1=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT1);
				var part2=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART2);
				hand1.setVisible(true);
				part2.setVisible(true);			
				hand1.setSpriteFrame("yyg_hand1.png");
			}, this),this.callNext);
			xierqiu.runAction(seq);	
		}else{
			var move=cc.moveTo(0.6, cc.p(933,563));
			var move1=cc.moveTo(0.6,cc.p(933,523));
			var seq=cc.sequence(move,move1,cc.callFunc(function() {
				xierqiu.setSpriteFrame("xierqiu2.png");
				yyg.yyg_lineUp(cc.p(21,210));
			},this),cc.callFunc(function() {
				var line =yyg.getChildByTag(TAG_YYG).getChildByTag(TAG_YYG_LINE);
				var move=cc.moveTo(0.9, cc.p(21,270));
				var move1=cc.moveTo(0.3, cc.p(21,284));
				var sca=cc.scaleTo(0.3,0.6,1);
				var spa=cc.spawn(move1,sca);
				var mov=cc.moveTo(0.6, cc.p(21,380));
				var seq=cc.sequence(cc.delayTime(2),move,move1,spa,mov);
				line.runAction(seq);
			}, this),cc.delayTime(4),cc.callFunc(function() {
				xierqiu.removeFromParent();
				var hand=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT);
				var part=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART);
				hand.setVisible(false);
				part.setVisible(false);
				var hand1=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT1);
				var part2=this.getChildByTag(TAG_YYG_YYG).getChildByTag(TAG_YYG1_RIGHT_PART2);
				hand1.setVisible(true);
				part2.setVisible(true);			
				hand1.setSpriteFrame("yyg_hand1.png");
			}, this),this.callNext);
			xierqiu.runAction(seq);
		}		
	},
	loadZhuixingping:function(pos){
		this.zhuixingping = new Zhuixingping(this);
		this.addChild(this.zhuixingping, 51);
		this.loadInLib(this.zhuixingping, pos, cc.p(555, 153.6));
	},
	loadFentai:function(pos){
		this.fentai = new Fentai(this);
		this.addChild(this.fentai, 20);
		this.loadInLib(this.fentai, pos, cc.p(700, 360));
	},
	loadJiajicheng:function(pos){
		this.jiajicheng = new Jiajicheng(this);
		this.addChild(this.jiajicheng, 20);
		this.loadInLib(this.jiajicheng, pos, cc.p(700, 360));
	},
	loadInLib:function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	},
	kill:function(obj){
		var fade = cc.fadeTo(0.5,0);
		var func = cc.callfunc(function(){
			obj.removeFromParent(true);
		},this);
		var seq = cc.sequence(fade,func);
		obj.runAction(seq)
	},

	flowNext:function(){
		gg.flow.next();
	},
	onExit:function(){
		this._super();
		this.callNext.release();
		this.callKill.release();
	}
});